package com.syaccp.model;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;

@SuppressWarnings("serial")
@TableName("sys_user")
public class SysUser implements java.io.Serializable, UserDetails {

	@TableId(value = "id", type = IdType.AUTO)
	private Integer id;

	private Integer deptId;

	private String deptName;

	private Integer roleId;

	private String roleName;

	private String username;

	private String name;

	private String userCode;



	private String email;

	private String phone;

	private Integer sex;

	private String avatar;
	@JsonIgnore
	private String password;

	private String loginIp;

	private Date loginDate;

	private Integer status;

	private Integer delFlag;

	private Integer createBy;

	private Date createTime;

	private Integer updateBy;

	@TableField(exist=false)
	private List<String> permissions;

	@JSONField(serialize = false)
	@JsonIgnore
	@TableField(exist=false)
	private List<SimpleGrantedAuthority> authorities;

	public List<String> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<String> permissions) {
		this.permissions = permissions;
	}

	@JsonIgnore
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (authorities != null) {
			return authorities;
		}
		// 把permissions中String类型的权限信息封装成SimpleGrantedAuthority对象
//		authorities = new ArrayList<>(permissions.size());
//		for (String permission : permissions) {
//			SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
//			authorities.add(authority);
//		}
		authorities = permissions.stream()
				.map(SimpleGrantedAuthority::new)
				.collect(Collectors.toList());

		return authorities;
	}


	@JsonIgnore
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}
	@JsonIgnore
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {

		this.deptName = deptName == null ? null : deptName.trim();
	}

	@JsonIgnore
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@JsonIgnore
	@Override
	public boolean isEnabled() {
		if (this.getStatus().equals(1)) {
			return false;
		} else {
			return true;
		}

	}

	@TableField(fill = FieldFill.INSERT_UPDATE)
	private Date updateTime;

	private String remark;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username == null ? null : username.trim();
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email == null ? null : email.trim();
	}



	public String getName() {
		return name;
	}

	public void setName(String name) {

		this.name = name == null ? null : name.trim();
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {

		this.userCode = userCode == null ? null : userCode.trim();
	}

	public String getPhone() {
		return phone;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {

		this.roleName = roleName == null ? null : roleName.trim();
	}

	public void setPhone(String phone) {
		this.phone = phone == null ? null : phone.trim();
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar == null ? null : avatar.trim();
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password == null ? null : password.trim();
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp == null ? null : loginIp.trim();
	}

	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(Integer delFlag) {
		this.delFlag = delFlag;
	}

	public Integer getCreateBy() {
		return createBy;
	}

	public void setCreateBy(Integer createBy) {
		this.createBy = createBy;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Integer getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(Integer updateBy) {
		this.updateBy = updateBy;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark == null ? null : remark.trim();
	}
}
